<template>
  <div class="main-layout">
    <!-- 顶部搜索区域 -->
    <header class="header">
      <HeaderView 
        v-model="searchQuery" 
        @search="handleSearch"
        @link-click="handleLinkClick"
      />
    </header>

    <!-- 主体内容区域 -->
    <div class="main-container">
      <!-- 左侧导航栏 -->
      <nav class="sidebar">
        <Sidebar 
          v-model="activeMenu" 
          @update:modelValue="handleMenuChange"
          @command="handleSidebarCommand"
        />
      </nav>

      <!-- 右侧内容区域 -->
      <main class="content">
        <router-view />
      </main>
    </div>
    
    <!-- 右下角刷新按钮 -->
    <div class="refresh-button" @click="handleRefresh">
      <el-icon><Refresh /></el-icon>
    </div>
  </div>
</template>

<script setup>
import { ref, watch, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useUserStore } from '@/store/user'
import { useNotificationStore } from '@/store/notification'
import { Refresh } from '@element-plus/icons-vue'
import HeaderView from '@/components/common/HeaderView.vue'
import Sidebar from '@/components/common/Sidebar.vue'

const router = useRouter()
const route = useRoute()
const userStore = useUserStore()
const notificationStore = useNotificationStore()
const searchQuery = ref('')
const activeMenu = ref('discover')

// 组件挂载时获取未读通知数量
onMounted(() => {
  if (userStore.isLoggedIn) {
    notificationStore.fetchUnreadCount()
  }
})

// 根据当前路由更新活动菜单
watch(() => route.path, (newPath) => {
  if (newPath === '/') activeMenu.value = 'discover'
  else if (newPath === '/notification') activeMenu.value = 'notification'
  else if (newPath === '/profile') activeMenu.value = 'profile'
  else if (newPath === '/publish') activeMenu.value = 'publish'
}, { immediate: true })

const handleSearch = (query) => {
  console.log('搜索:', query)
  // 可以在这里处理搜索逻辑，例如跳转到搜索页面
  // router.push(`/search?q=${encodeURIComponent(query)}`)
}

const handleLinkClick = (key) => {
  switch (key) {
    case 'business':
      console.log('跳转到业务合作页面')
      // 可以在这里处理业务合作页面跳转
      window.open('https://www.xiaohongshu.com/business', '_blank')
      break
    case 'creation':
      console.log('跳转到创作中心页面')
      // 可以在这里处理创作中心页面跳转
      window.open('https://creator.xiaohongshu.com/', '_blank')
      break
  }
}

const handleMenuChange = (menuKey) => {
  activeMenu.value = menuKey
  
  // 需要登录的菜单项
  const requireLogin = ['publish', 'notification', 'profile']
  if (requireLogin.includes(menuKey) && !userStore.isLoggedIn) {
    router.push('/login')
    return
  }
  
  // 根据菜单项跳转到对应路由
  switch (menuKey) {
    case 'discover':
      router.push('/')
      break
    case 'notification':
      router.push('/notification')
      break
    case 'profile':
      router.push('/profile')
      break
    case 'publish':
      router.push('/publish')
      break
  }
}

const handleSidebarCommand = (command) => {
  switch (command) {
    case 'logout':
      console.log('退出登录')
      userStore.userLogout()
      router.push('/login')
      break
    case 'about':
      console.log('关于小红书')
      // 可以在这里处理关于页面逻辑
      break
  }
}

const handleRefresh = () => {
  // 刷新当前页面
  window.location.reload()
}
</script>

<style lang="scss" scoped>
.main-layout {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-color: #fff;
}

.header {
  height: 72px;
  background-color: #fff;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
}

.main-container {
  display: flex;
  flex: 1;
  margin-top: 72px;
  width: 100%;
  position: relative;
}

.sidebar {
  width: 200px;
  min-width: 200px;
  height: calc(100vh - 72px);
  background-color: #fff;
  position: fixed;
  top: 72px;
  left: 48px;
  bottom: 0;
  display: flex;
  flex-direction: column;
  padding-top: 16px;
}

.content {
  flex: 1;
  margin-left: 248px;
  min-height: calc(100vh - 72px);
  padding: 24px 48px;
  background-color: #fff;
  max-width: calc(100% - 248px);
}

// 响应式设计
@media (max-width: 768px) {
  .sidebar {
    width: 240px;
    min-width: 240px;
  }
  
  .content {
    margin-left: 240px;
    padding: 16px;
  }
}

// 刷新按钮样式
.refresh-button {
  position: fixed;
  bottom: 30px;
  right: 30px;
  width: 50px;
  height: 50px;
  background: #ff2442;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(255, 36, 66, 0.3);
  transition: all 0.3s ease;
  z-index: 1000;
  
  &:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 16px rgba(255, 36, 66, 0.4);
  }
  
  .el-icon {
    color: white;
    font-size: 20px;
  }
}
</style>